﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using EHR.Common;
using CommunitySystem.PageView.晋升管理;
using System.Collections.Generic;
using CommunitySystem.Infrastructure.CommonMethod;
using CommunitySystem.PageView;
using CommunitySystem.Ctrls;
using System.IO;
using CommunitySystem.JinShengService;
using CommunitySystem.PublicOfficialSelectionService;
using System.Collections.ObjectModel;
using System.Windows.Data;

namespace CommunitySystem
{
	public partial class JinshengListControl1 : UserControl
	{
        private readonly JinShengService.JinShengServiceClient _client = null;
        private PublicOfficialSelectionServiceClient downLoadClient = new PublicOfficialSelectionServiceClient();

        private string LoginRole = "";
        public void InitQuery()
        {
            for (int i = DateTime.Today.Year; i > DateTime.Today.Year - 3; i--)
            {
                ComboBoxItem cbi = new ComboBoxItem();
                cbi.Content = i.ToString();
                cbi.Tag = i;
                cbyear.Items.Add(cbi);
            }
            cbyear.SelectedIndex = 0;

            List<string> statuss = new List<string>() { "全部", "办理中", "已办结", "未通过" };
            for (int i = 0; i < statuss.Count; i++)
            {
                 ComboBoxItem cbi = new ComboBoxItem();
                cbi.Content = statuss[i].ToString();
                cbi.Tag = statuss[i].ToString();
                cbstatus.Items.Add(cbi);
            }
            cbstatus.SelectedIndex = 1;
        }
        public JinshengListControl1()
		{
			// 为初始化变量所必需
			InitializeComponent();

            

            InitQuery();

            IsWorking.Visibility = Visibility.Visible;
            busy.BusyContent = "正在同步数据，请稍后...";

            _client = new JinShengService.JinShengServiceClient();
            _client.GetOrgForHRCompleted += (sender, e) =>
            {
                LoginRole = e.Result;
                if (JinShenConst.Administrator.Contains(e.Result))
                {
                    btnAddCandidator.Visibility = System.Windows.Visibility.Visible;
                    btnCheck.Visibility = System.Windows.Visibility.Visible;
                    btnCheckCandidator.Visibility = System.Windows.Visibility.Visible;
                    btnDelete.Visibility = System.Windows.Visibility.Visible;
                    btnEdit.Visibility = System.Windows.Visibility.Visible;
                    btnKaoCha.Visibility = System.Windows.Visibility.Visible;
                    btnNew.Visibility = System.Windows.Visibility.Visible;
                    //btnSelectCandidator.Visibility = System.Windows.Visibility.Visible;
                    btnValation.Visibility = System.Windows.Visibility.Visible;

                    this.btnDeleteGW.Visibility = Visibility.Visible;
                }
                if (JinShenConst.HR.Contains(e.Result))
                {
                    btnAddCandidator.Visibility = System.Windows.Visibility.Visible;
                    //btnCheck.Visibility = System.Windows.Visibility.Visible;
                    btnDelete.Visibility = System.Windows.Visibility.Visible;
                    btnEdit.Visibility = System.Windows.Visibility.Visible;
                    btnKaoCha.Visibility = System.Windows.Visibility.Visible;
                    btnNew.Visibility = System.Windows.Visibility.Visible;
                    btnSelectCandidator.Visibility = System.Windows.Visibility.Visible;
                    btnValation.Visibility = System.Windows.Visibility.Visible;

                    this.btnDeleteGW.Visibility = Visibility.Collapsed;
                }
            };
            _client.GetOrgForHRAsync(Session.LoginUser.LoginName);

            //_client.GetJinShen_MainInfosByCountAsync(string.Format(" and year(jsm.CreateDate)='{0}'", (this.cbyear.SelectedItem as ComboBoxItem).Content), Session.LoginUser.LoginName);
            _client.GetJinShen_MainInfosByCountCompleted += new EventHandler<GetJinShen_MainInfosByCountCompletedEventArgs>(_client_GetJinShen_MainInfosByCountCompleted);
            Query();
            _client.GetJinShen_MainInfosByConCompleted += new EventHandler<GetJinShen_MainInfosByConCompletedEventArgs>(_client_GetJinShen_MainInfosByConCompleted);
            //_client.GetJinShen_MainInfosCompleted += new EventHandler<JinShengService.GetJinShen_MainInfosCompletedEventArgs>(_client_GetJinShen_MainInfosCompleted);
           
            _client.DeleteJinShenMainInfosCompleted += (sender, e) =>
            {
                if (e.Error == null)
                {
                    SilverlightMessageBoxLibrary.Message.InfoMessage("删除成功!");
                  //  Refresh();
                    Query();
                }
                else
                {
                    SilverlightMessageBoxLibrary.Message.InfoMessage("删除失败!");
                }
            };

        
           

            //Refresh();
		}

        void _client_GetJinShen_MainInfosByConCompleted(object sender, GetJinShen_MainInfosByConCompletedEventArgs e)
        {
            dg.ItemsSource = null;
            dg.ItemsSource = e.Result;

            IsWorking.Visibility = Visibility.Collapsed;
        }
        private int pageSize = 20;
        void _client_GetJinShen_MainInfosByCountCompleted(object sender, GetJinShen_MainInfosByCountCompletedEventArgs e)
        {
            ObservableCollection<int> list = new ObservableCollection<int>();
            int last = e.Result % pageSize;
            int count = 0;
            if (last > 0)
                count = e.Result / pageSize + 1;
            else
                count = e.Result / pageSize;
            for (int i = 0; i < count; i++)
            {
                list.Add(i);
            }

            PagedCollectionView pcv = new PagedCollectionView(list);
            dataPager1.PageSize = 1;
            dataPager1.Source = null;
            dataPager1.Source = pcv;
        }

        void _client_GetJinShen_MainInfosCompleted(object sender, JinShengService.GetJinShen_MainInfosCompletedEventArgs e)
        {
            dg.ItemsSource = null;
            dg.ItemsSource = e.Result;

            IsWorking.Visibility = Visibility.Collapsed;
        }

        private void Refresh()
        {
            _client.GetJinShen_MainInfosAsync("1=1 ", Session.LoginUser.LoginName);
        }

   

        private PromotWindow window = null;

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void imageButton1_Click(object sender, RoutedEventArgs e)
        {


            if (JinShenConst.CanAction("", JinShenConst.ACTION_Solution_New))
            {
                var orgid = Session.LoginUser.OrgCode;
              List<string> orgids=  new List<string>();
                foreach (var item in orgid.Split(','))
	                {
		                 orgids.Add(item);
	                }
                AddjinshengWindow win = new AddjinshengWindow(0, JinShenConst.ACTION_Solution_New, orgids);
                win.Show();
                win.Closed += new EventHandler(win_Closed);
            }
            else
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("当前状态不能进行" + JinShenConst.ACTION_Solution_New + "操作");
            }           
        }

        void win_Closed(object sender, EventArgs e)
        {
           // Refresh();
            Query();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void window_Closed(object sender, EventArgs e)
        {
          //  Refresh();

            Query();

        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void imageButton2_Click(object sender, RoutedEventArgs e)
        {
            if (dg.SelectedItem != null)
            {
                var item = dg.SelectedItem as JinShenMHelp;

                if (JinShenConst.CanAction(item.Status, JinShenConst.ACTION_Solution_Modify) || item.Status.Equals("再次申请"))
                {
                    //window = new PromotWindow(item.ID, JinShenConst.ACTION_Solution_Modify);
                    //window.Show();
                    //window.Closed += new EventHandler(window_Closed);
                    var orgid = Session.LoginUser.OrgCode;
                    List<string> orgids = new List<string>();
                    foreach (var item1 in orgid.Split(','))
                    {
                        orgids.Add(item1);
                    }
                    EditJinshengWindow win = new EditJinshengWindow(item, JinShenConst.ACTION_Solution_Modify,orgids);
                    win.Show();
                   win.Closed+=new EventHandler(win_Closed);
                }
                else
                {
                    SilverlightMessageBoxLibrary.Message.InfoMessage("当前状态为" + item.Status + ",不能进行" + JinShenConst.ACTION_Solution_Modify + "操作");
                }

                /////只有处于提交和退回状态，才可以编辑
                //if (item.Status == CommunitySystem.PageView.JinShenConst.STATUS_Solution_Submit || item.Status == CommunitySystem.PageView.JinShenConst.STATUS_Solution_Retire)
                //{
                //    window = new PromotWindow(item.ID, "编辑");
                //    window.Show();
                //    window.Closed += new EventHandler(window_Closed);
                //}
                //else
                //{
                //    SilverlightMessageBoxLibrary.Message.InfoMessage(item.Status + "状态不能编辑");
                //}
                
            }
        }


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void imageButton3_Click(object sender, RoutedEventArgs e)
        {
            ///只有提交和退回状态能删除
            if (dg.SelectedItem != null)
            {
                var item = dg.SelectedItem as JinShenMHelp;
                ImageButton ib = sender as ImageButton;
                if (JinShenConst.CanAction(item.Status, ib.IBTitle) || item.Status.Equals("再次申请"))
                {
                    SilverlightMessageBoxLibrary.CustomMessage cm = new SilverlightMessageBoxLibrary.CustomMessage("确认删除?", SilverlightMessageBoxLibrary.CustomMessage.MessageType.Confirm, null);
                    cm.Show();
                    cm.OKButton.Click += (sender1, e1) =>
                    {
                        _client.DeleteJinShenMainInfosAsync(item);
                    };
                }
                else
                {
                    SilverlightMessageBoxLibrary.Message.InfoMessage("当前状态为"+item.Status+",不能进行" + ib.IBTitle + "操作");
                }

                //if (item.Status == CommunitySystem.PageView.JinShenConst.STATUS_Solution_Submit || item.Status == CommunitySystem.PageView.JinShenConst.STATUS_Solution_Retire)
                //{
                //    SilverlightMessageBoxLibrary.CustomMessage cm = new SilverlightMessageBoxLibrary.CustomMessage("确认删除?", SilverlightMessageBoxLibrary.CustomMessage.MessageType.Confirm, null);
                //    cm.Show();
                //    cm.OKButton.Click += (sender1, e1) =>
                //    {
                //        _client.DeleteJinShenMainInfoAsync(item);
                //    };
                //}
                //else
                //{
                //    SilverlightMessageBoxLibrary.Message.ErrorMessage("状态为" + item.Status + "方案不能删除");
                //    return;
                //}
            }
        }

       /// <summary>
       /// 审核
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
        private void imageButton4_Click(object sender, RoutedEventArgs e)
        {

            ///只有状态为提交 和 审核通过 或者审核失败， 才能审核
            if (dg.SelectedItem != null)
            {
                var item = dg.SelectedItem as JinShenMHelp;
                ImageButton ib = sender as ImageButton;
                if ((JinShenConst.CanAction(item.Status, ib.IBTitle) || item.Status.Equals("再次申请")) && JinShenConst.Administrator.Contains(LoginRole))
                //if (JinShenConst.CanAction(item.Status, ib.IBTitle))
                {
                    //PromotCheckWindow wind = new PromotCheckWindow(item);
                    //wind.Show();

                    EditJinshengWindow win = new EditJinshengWindow(item, JinShenConst.ACTION_Solution_Check);
                    win.Show();
                    win.Closed += new EventHandler(win_Closed);
                }
                else
                {
                    SilverlightMessageBoxLibrary.Message.InfoMessage("当前状态不能进行" + ib.IBTitle + "操作");
                }
                //if (item.Status == CommunitySystem.PageView.JinShenConst.STATUS_Solution_Submit 
                //    || item.Status == CommunitySystem.PageView.JinShenConst.STATUS_Solution_Retire 
                //    || item.Status == CommunitySystem.PageView.JinShenConst.STATUS_SolutionPass)
                //{
                //    PromotCheckWindow wind = new PromotCheckWindow(item);
                //    wind.Show();
                //}
                //else
                //{
                //    SilverlightMessageBoxLibrary.Message.ErrorMessage("状态为" + item.Status + "方案不能审核");
                //    return;
                //}
            }
         
        }

        /// <summary>
        /// 增加候选人
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ImageButton_Click(object sender, RoutedEventArgs e)
        {
            if (dg.SelectedItem != null)
            {
                var item = dg.SelectedItem as JinShenMHelp;
                ImageButton ib = sender as ImageButton;
                if (JinShenConst.CanAction(item.Status, ib.IBTitle) && JinShenConst.HR.Contains(LoginRole))
              //  if (JinShenConst.CanAction(item.Status, ib.IBTitle) || item.Status.Equals("再次人员审核"))
                {

                    //window = new PromotWindow(item.ID, "新增候选人");
                    //window.Show();
                    //window.Closed += new EventHandler(window_Closed);
                    EditJinshengWindow win = new EditJinshengWindow(item, JinShenConst.ACTION_Candidate_New);
                    win.Show();
                    win.Closed += new EventHandler(win_Closed);
                }
                else
                {
                    SilverlightMessageBoxLibrary.Message.InfoMessage("当前状态不能进行" + ib.IBTitle + "操作");
                }
                /////只有审核通过 和  人员审核不通过可以进入增加
                //if (item.Status == CommunitySystem.PageView.JinShenConst.STATUS_SolutionPass)
                //{
                //    window = new PromotWindow(item.ID, "新增候选人");
                //    window.Show();
                //    window.Closed += new EventHandler(window_Closed);
                //}
                //else
                //{
                //    SilverlightMessageBoxLibrary.Message.ErrorMessage("状态为" + item.Status + "方案不能增加候选人");
                //    return;
                //}
            }
        }


        /// <summary>
        /// 人员审核
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCheckCandidator_Click(object sender, RoutedEventArgs e)
        {
            if (dg.SelectedItem != null)
            {
                var item = dg.SelectedItem as JinShenMHelp;
                ImageButton ib = sender as ImageButton;
                if ((JinShenConst.CanAction(item.Status, ib.IBTitle) || item.Status.Equals("再次人员审核")) && JinShenConst.Administrator.Contains(LoginRole))
               // if (JinShenConst.CanAction(item.Status, ib.IBTitle))
                {
                    //PromotCheckWindow wind = new PromotCheckWindow(item);
                    //wind.Show();
                    EditJinshengWindow win = new EditJinshengWindow(item, JinShenConst.ACTION_Candidate_Check);
                    win.Show();
                    win.Closed += new EventHandler(win_Closed);
                }
                else
                {
                    SilverlightMessageBoxLibrary.Message.InfoMessage("当前状态不能进行" + ib.IBTitle + "操作");
                }

            }
        }
        /// <summary>
        /// 民主测评及竞争上岗
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ImageButton_Click_1(object sender, RoutedEventArgs e)
        {
            ///候选人审核 状态才可以
            if (dg.SelectedItem != null)
            {
                var item = dg.SelectedItem as JinShenMHelp;
                ImageButton ib = sender as ImageButton;
                if ((JinShenConst.CanAction(item.Status, ib.IBTitle) || item.Status.Equals("主管部门查看")) && (JinShenConst.HR.Contains(LoginRole) || item.Status.Equals("主管部门查看")))
               // if (JinShenConst.CanAction(item.Status, ib.IBTitle))
                {
                    //CandidatorCheckWindow window = new CandidatorCheckWindow(item);
                    //window.Show();
                    EditJinshengWindow win = new EditJinshengWindow(item, JinShenConst.ACTION_Candidate_Evalution);
                    win.Show();
                    win.Closed += new EventHandler(win_Closed);
                }
                else
                {
                    SilverlightMessageBoxLibrary.Message.InfoMessage("当前状态不能进行" + ib.IBTitle + "操作");
                }
                //if (item.Status == CommunitySystem.PageView.JinShenConst.STATUS_CandidateCheck_Pass || item.Status == CommunitySystem.PageView.JinShenConst.STATUS_Candidate_Approved)
                //{
                //    CandidatorCheckWindow window = new CandidatorCheckWindow(item);
                //    window.Show();
                //}
                //else
                //{
                //    SilverlightMessageBoxLibrary.Message.ErrorMessage("状态为" + item.Status + "方案不能进行民主测评");
                //    return;
                //}
            }
        }

        /// <summary>
        /// 人员核定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ImageButton_Click_2(object sender, RoutedEventArgs e)
        {
            if (dg.SelectedItem != null)
            {
                var item = dg.SelectedItem as JinShenMHelp;
                ImageButton ib = sender as ImageButton;
                if ((JinShenConst.CanAction(item.Status, ib.IBTitle) && JinShenConst.HR.Contains(LoginRole)) || item.Status.Equals("已查看"))
               // if (JinShenConst.CanAction(item.Status, ib.IBTitle))
                {
                    //ChooseCandidatorWindow window = new ChooseCandidatorWindow(item);

                    //window.Show();

                    EditJinshengWindow win = new EditJinshengWindow(item, JinShenConst.ACTION_Candidate_Approval);
                    win.Show();
                    win.Closed += new EventHandler(win_Closed);
                }
                else
                {
                    SilverlightMessageBoxLibrary.Message.InfoMessage("当前状态不能进行" + ib.IBTitle + "操作");
                }
                //if (item.Status == CommunitySystem.PageView.JinShenConst.STATUS_SolutionPass || item.Status == CommunitySystem.PageView.JinShenConst.STATUS_CandidateCheck_UnPass)
                //{
                //    ChooseCandidatorWindow window = new ChooseCandidatorWindow(item);
                    
                //    window.Show();
                //}
                //else
                //{
                //    SilverlightMessageBoxLibrary.Message.ErrorMessage("状态为" + item.Status + "方案不能进行民主测评");
                //    return;
                //}
            }
           

        }

        /// <summary>
        /// 考察管理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void imgBtn8_Click(object sender, RoutedEventArgs e)
        {
            if (dg.SelectedItem != null)
            {
                var item = dg.SelectedItem as JinShenMHelp;
                ImageButton ib = sender as ImageButton;
                if (JinShenConst.CanAction(item.Status, JinShenConst.ACTION_Candidate_Inspect) || item.Status.Equals("退回") || item.Status.Equals("终止办理"))
                {
                    if (item.JinShen_MainInfos.Count == 1)
                    {
                        FlipSwitchForJinShen jinshen = new FlipSwitchForJinShen(item.JinShen_MainInfos[0]);
                        CommunitySystem.Infrastructure.CommonMethod.TabControlMethod.
                             addNewTabItem(jinshen, "考察管理", item.ID + "考察管理", App.currentTabControl,
                                           App.Current.Resources["tabItemStyle"] as Style);
                    }
                    else
                    {
                        FlipSwitchForJinShens jinshen = new FlipSwitchForJinShens(item.JinShen_MainInfos);
                        CommunitySystem.Infrastructure.CommonMethod.TabControlMethod.
                             addNewTabItem(jinshen, "考察管理", item.ID + "考察管理", App.currentTabControl,
                                           App.Current.Resources["tabItemStyle"] as Style);
                    }

                }
                else
                {
                    SilverlightMessageBoxLibrary.Message.InfoMessage("当前状态不能进行" + ib.IBTitle + "操作");
                }
                //if (item.Status == CommunitySystem.PageView.JinShenConst.STATUS_Candidate_Approved
                //    || item.Status == CommunitySystem.PageView.JinShenConst.STATUS_Candidate_Inspect
                //    || item.Status == CommunitySystem.PageView.JinShenConst.STATUS_Party_Meeting
                //    || item.Status == CommunitySystem.PageView.JinShenConst.STATUS_Candidate_Show
                //    || item.Status == CommunitySystem.PageView.JinShenConst.STATUS_Promot_Apply
                //    || item.Status == CommunitySystem.PageView.JinShenConst.STATUS_Promot_Agree)
                //{
                //    FlipSwitchForJinShen jinshen = new FlipSwitchForJinShen(item);
                //    CommunitySystem.Infrastructure.CommonMethod.TabControlMethod.
                //         addNewTabItem(jinshen, "考察管理", item.ID + "考察管理", App.currentTabControl,
                //                       App.Current.Resources["tabItemStyle"] as Style);
                //}
                //else
                //{
                //    SilverlightMessageBoxLibrary.Message.ErrorMessage("状态为" + item.Status + "方案不能进行民主测评");
                //    return;
                //}
            }
            
        }

        
        private List<byte> fileContent = new List<byte>();
        /// <summary>
        /// 方案下载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        SaveFileDialog openFile;
        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton hbtn = sender as HyperlinkButton;

            var attach = hbtn.Tag as JinShenMHelp;
          
            SilverlightMessageBoxLibrary.CustomMessage cm = new SilverlightMessageBoxLibrary.CustomMessage("确认要下载此方案吗?", SilverlightMessageBoxLibrary.CustomMessage.MessageType.Confirm, null);
            cm.Show();
            cm.OKButton.Click += (sender1, e1) =>
            {

                string extend = attach.realname.Substring(attach.realname.LastIndexOf('.'));
               // var openFile = new SaveFileDialog() { Filter = string.Format("{0}|*.{0}", extend) };
                openFile = new SaveFileDialog() { Filter = string.Format("{0}|*.{0}", extend) };
                if (openFile.ShowDialog() == true)
                {
                    fileContent.Clear();
                    //downLoadClient.DownLoadFileCompleted -= new EventHandler<PublicOfficialSelectionService.DownLoadFileCompletedEventArgs>(downLoadClient_DownLoadFileCompleted);
                    //downLoadClient.DownLoadFileCompleted += new EventHandler<PublicOfficialSelectionService.DownLoadFileCompletedEventArgs>(downLoadClient_DownLoadFileCompleted);

                    //downLoadClient.DownLoadFileAsync(new PublicOfficialSelectionService.DownLoadFile() { PathKey = "file", HasFinished = false, Lenght = 1600, FileName = attach.filename, StartPosition = 0 }, openFile.OpenFile());

                    IsWorking.Visibility = Visibility.Visible;
                    busy.BusyContent = "正在下载数据，请稍后...";

                    _client.DownLoadFile1Completed -= new EventHandler<JinShengService.DownLoadFile1CompletedEventArgs>(_client_DownLoadFile1Completed);
                    _client.DownLoadFile1Completed += new EventHandler<JinShengService.DownLoadFile1CompletedEventArgs>(_client_DownLoadFile1Completed);

                    _client.DownLoadFile1Async(attach.filename);
                }
           
            };
        }

        void _client_DownLoadFile1Completed(object sender, JinShengService.DownLoadFile1CompletedEventArgs e)
        {
            Stream myStream;
            if ((myStream = openFile.OpenFile()) != null)
            {
                using (BinaryWriter bw = new BinaryWriter(myStream))
                {
                   
                    bw.Write(e.Result);
                    myStream.Dispose();
                    myStream.Close();
                    SilverlightMessageBoxLibrary.Message.InfoMessage("下载完毕!");
                    IsWorking.Visibility = Visibility.Collapsed;
                }

            }
        }
     
        void downLoadClient_DownLoadFileCompleted(object sender, PublicOfficialSelectionService.DownLoadFileCompletedEventArgs e)
        {
            var down = e.Result;
            fileContent.AddRange(down.Content);
            if (down.HasFinished == true)
            {
                var stream = e.UserState as Stream;
                stream.Write(fileContent.ToArray(), 0, fileContent.Count);
                stream.Dispose();
                stream.Close();
                SilverlightMessageBoxLibrary.Message.InfoMessage("下载完毕!");
            }
            else
            {
                down.StartPosition += down.Lenght;
                downLoadClient.DownLoadFileAsync(down, e.UserState);
            }
        }
        /// <summary>
        /// 操作事项
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HyperlinkButton_Click_1(object sender, RoutedEventArgs e)
        {
            HyperlinkButton hbtn = sender as HyperlinkButton;

            var attach = hbtn.Tag as JinShenMHelp;

            if (JinShenConst.ACTION_Solution_Check.Equals(hbtn.Content.ToString())) //审核
            {
                if ((JinShenConst.CanAction(attach.Status, hbtn.Content.ToString()) || attach.Status.Equals("再次申请")) && JinShenConst.Administrator.Contains(LoginRole))
                {
                    EditJinshengWindow win = new EditJinshengWindow(attach, JinShenConst.ACTION_Solution_Check);
                    win.Show();
                    win.Closed += new EventHandler(win_Closed);
                }
                else
                {
                    SilverlightMessageBoxLibrary.Message.InfoMessage("当前状态不能进行" + hbtn.Content.ToString() + "操作");
                }
            }

            else if (JinShenConst.ACTION_Candidate_New.Equals(hbtn.Content.ToString()) ) //增加人员
            {
                if (JinShenConst.CanAction(attach.Status, hbtn.Content.ToString()) && JinShenConst.HR.Contains(LoginRole))
                {
                    EditJinshengWindow win = new EditJinshengWindow(attach, JinShenConst.ACTION_Candidate_New);
                    win.Show();
                    win.Closed += new EventHandler(win_Closed);
                }
                else
                {
                    SilverlightMessageBoxLibrary.Message.InfoMessage("当前状态不能进行" + hbtn.Content.ToString() + "操作");
                }
            }
            else if (JinShenConst.ACTION_Candidate_Check.Equals(hbtn.Content.ToString()) || attach.Status.Equals("再次人员审核")) //人员审核
            {
                if ((JinShenConst.CanAction(attach.Status, hbtn.Content.ToString()) || attach.Status.Equals("再次人员审核")) && JinShenConst.Administrator.Contains(LoginRole))
                {

                    EditJinshengWindow win = new EditJinshengWindow(attach, JinShenConst.ACTION_Candidate_Check);
                    win.Show();
                    win.Closed += new EventHandler(win_Closed);
                }
                else
                {
                    SilverlightMessageBoxLibrary.Message.InfoMessage("当前状态不能进行" + hbtn.Content.ToString() + "操作");
                }
            }
            else if (JinShenConst.ACTION_Candidate_Evalution.Equals(hbtn.Content.ToString()) ) //民主测评
            {
                if ((JinShenConst.CanAction(attach.Status, hbtn.Content.ToString()) || attach.Status.Equals("主管部门查看")) && (JinShenConst.HR.Contains(LoginRole) || attach.Status.Equals("主管部门查看")))
                {
                    EditJinshengWindow win;

                    if (attach.Status.Equals("主管部门查看"))
                        win = new EditJinshengWindow(attach, JinShenConst.ACTION_Candidate_Evalution, attach.Status);
                    else
                    win = new EditJinshengWindow(attach, JinShenConst.ACTION_Candidate_Evalution);

                    win.Show();
                    win.Closed += new EventHandler(win_Closed);

                }
                else
                {
                    SilverlightMessageBoxLibrary.Message.InfoMessage("当前状态不能进行" + hbtn.Content.ToString() + "操作");
                }
            }
            else  if (JinShenConst.ACTION_Candidate_Approval.Equals(hbtn.Content.ToString())) //人员核定
            {
                if ((JinShenConst.CanAction(attach.Status, hbtn.Content.ToString()) && JinShenConst.HR.Contains(LoginRole)) || attach.Status.Equals("已查看"))
                {
                    EditJinshengWindow win = new EditJinshengWindow(attach, JinShenConst.ACTION_Candidate_Approval);
                    win.Show();
                    win.Closed += new EventHandler(win_Closed);
                }
                else
                {
                    SilverlightMessageBoxLibrary.Message.InfoMessage("当前状态不能进行" + hbtn.Content.ToString() + "操作");
                }
            }
            else  //人员考察
            {
                if (JinShenConst.CanAction(attach.Status, JinShenConst.ACTION_Candidate_Inspect) || attach.Status.Equals("退回"))
                {
                    if (attach.JinShen_MainInfos.Count == 1)
                    {
                        FlipSwitchForJinShen jinshen = new FlipSwitchForJinShen(attach.JinShen_MainInfos[0]);
                        CommunitySystem.Infrastructure.CommonMethod.TabControlMethod.
                             addNewTabItem(jinshen, "考察管理", attach.ID + "考察管理", App.currentTabControl,
                                           App.Current.Resources["tabItemStyle"] as Style);
                    }
                    else
                    {
                        FlipSwitchForJinShens jinshen = new FlipSwitchForJinShens(attach.JinShen_MainInfos);
                        CommunitySystem.Infrastructure.CommonMethod.TabControlMethod.
                             addNewTabItem(jinshen, "考察管理", attach.ID + "考察管理", App.currentTabControl,
                                           App.Current.Resources["tabItemStyle"] as Style);
                    }

                }
                else
                {
                    SilverlightMessageBoxLibrary.Message.InfoMessage("当前状态不能进行" + hbtn.Content.ToString() + "操作");
                }
            }

        }

        private void dg_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            HyperlinkButton hb = dg.Columns[4].GetCellContent(e.Row).FindName("hbtn") as HyperlinkButton;

            var jshelp = e.Row.DataContext as JinShenMHelp;
            if (JinShenConst.Administrator.Contains(LoginRole))
            {
                if (jshelp.OpStatus.Equals("晋升方案审核") || jshelp.OpStatus.Equals("人员审核") || jshelp.OpStatus.Equals("晋升办理") || jshelp.OpStatus.Equals("已办理") || jshelp.Status.Equals("主管部门查看"))
                    hb.Content = jshelp.OpStatus;
                else hb.Content = "";
            }
            if (JinShenConst.HR.Contains(LoginRole))
            {
                if (jshelp.OpStatus.Equals("晋升方案审核") || jshelp.OpStatus.Equals("人员审核") || jshelp.OpStatus.Equals("晋升办理") || jshelp.Status.Equals("主管部门查看"))
                    hb.Content = "";
                else hb.Content = jshelp.OpStatus;
            }
        }
      

        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            Query();
        }

        private void Query()
        {
            string tj = "";
            if ((this.cbstatus.SelectedItem as ComboBoxItem).Content.ToString() == "办理中")
                tj = string.Format(" and jsm.status not in ('晋升成功','终止办理')"); //'审核不通过','人员审核不通过',
            if ((this.cbstatus.SelectedItem as ComboBoxItem).Content.ToString() == "已办结")
                tj = string.Format(" and jsm.status  in ('晋升成功')");
            if ((this.cbstatus.SelectedItem as ComboBoxItem).Content.ToString() == "未通过")
                tj = string.Format(" and jsm.status  in ('终止办理')");  //'审核不通过','人员审核不通过',

            _client.GetJinShen_MainInfosByCountAsync(string.Format(" and year(jsm.CreateDate)='{0}' {1}", (this.cbyear.SelectedItem as ComboBoxItem).Content, tj), Session.LoginUser.LoginName);

        }

        private void dataPager1_PageIndexChanged(object sender, EventArgs e)
        {
            int index = dataPager1.PageIndex == -1 ? 0 : dataPager1.PageIndex;
            string tj = "";
            if ((this.cbstatus.SelectedItem as ComboBoxItem).Content.ToString() == "办理中")
                tj = string.Format(" and jsm.status not in ('晋升成功','终止办理')");  //'审核不通过','人员审核不通过',
            if ((this.cbstatus.SelectedItem as ComboBoxItem).Content.ToString() == "已办结")
                tj = string.Format(" and jsm.status  in ('晋升成功')");
            if ((this.cbstatus.SelectedItem as ComboBoxItem).Content.ToString() == "未通过")
                tj = string.Format(" and jsm.status  in ('终止办理')");//'审核不通过','人员审核不通过',
           
            _client.GetJinShen_MainInfosByConAsync(string.Format(" and year(jsm.CreateDate)='{0}' {1}", (this.cbyear.SelectedItem as ComboBoxItem).Content,tj), Session.LoginUser.LoginName, index + 1, pageSize);
            //.GetZhaoLu_DaoRuKaoShengModelByConAsync(string.Format(" and [year]={0}", tiaojian), index + 1, pageSize);
        }

        private void btnStatus_Click(object sender, RoutedEventArgs e)
        {
            //  终止办理  状态
            var item = dg.SelectedItem as JinShenMHelp;

           
            SilverlightMessageBoxLibrary.CustomMessage cm = new SilverlightMessageBoxLibrary.CustomMessage("确定要终止办理吗?", SilverlightMessageBoxLibrary.CustomMessage.MessageType.Confirm, null);
            cm.Show();
            cm.OKButton.Click += (sender1, e1) =>
            {
                _client.UpdateJinShenMStatusCompleted -= new EventHandler<UpdateJinShenMStatusCompletedEventArgs>(_client_UpdateJinShenMStatusCompleted);
                _client.UpdateJinShenMStatusCompleted += new EventHandler<UpdateJinShenMStatusCompletedEventArgs>(_client_UpdateJinShenMStatusCompleted);
          
                _client.UpdateJinShenMStatusAsync(item.ID, "终止办理");
            };
          
        }

        void _client_UpdateJinShenMStatusCompleted(object sender, UpdateJinShenMStatusCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("办理成功!");
                Query();
            }
            else
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("办理失败!");
            }
        }


        /// <summary>
        /// 办理记录查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void hbtnbljl_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton hbtn = sender as HyperlinkButton;
            var jsM = hbtn.Tag as JinShenMHelp;

            SelectJSSHDetailWindow swin = new SelectJSSHDetailWindow(jsM);
            swin.Show();
        }
        /// <summary>
        /// 删除方案中的岗位
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDeleteGW_Click(object sender, RoutedEventArgs e)
        {
            var item = dg.SelectedItem as JinShenMHelp;
            DelFAGWWindow win = new DelFAGWWindow(item);
            win.Show();
            win.Closed+=new EventHandler(win_Closed);

        }
    }
}